namespace CGF
{
    public partial class Tip : UIComponentBase, IAwake<UILayer, bool>
    {
        private string text;
        private float duration;

        public static void Open(string text, float duration = 2)
        {
            UIManagerComponent.instance.OpenUI<Tip>(UILayer.Tip, RootComponent.instance, true).SetText(text, duration);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<Tip>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/Common/Tip", layer, isFloat);
            BindProperties(transform);

            if (text != null)
            {
                SetText(text, duration);
            }

            ScaleIn(transform);
        }

        private async void SetText(string text, float duration)
        {
            this.text = text;
            this.duration = duration;
            if (!Text) return;
            Text.text = text;
            await CTask.Wait(duration);
            Close();
        }
    }
}
